<?php
	require_once "include/User.php";
	require_once "include/Article.php";
	require_once "include/Template.php";
	require_once "include/backend/ArticlesDb.php";
	
	 // creating template and setting the possibly logged in user
	$page = new Template();
	session_start();
	$page->setUser($_SESSION['user']);

	if ($_GET['err'] == "no_results") {
		$page->addContentFromFile("include/html/status/noresultsfound.html");
		echo $page->getHTML();
		die();
	}
	
	$nr = $_GET['page'];
	if ($nr == null)
		$nr = 0;
		
	// getting articles from the database
	$articles = new ArticlesDb();
	$keywords = explode(" ", $_GET['keywords']);
	$result = $articles->search($keywords, $nr);
	
	$html = "";
	for ($i=0; $i < ($result[$i]['max_articles']/8); $i++) {
		$html .= "<a href=\"search.php?page=".$i."&keywords=".$_GET['keywords']."\">".$i."</a> ";
	}
	$page->addContent($html);
	
	// adding them to the template
	for ($i = 0; $i < count($result); $i++) {
		$article = new Article($result[$i], true);
		$page->addContent($article->getHTML());
	}
	
	$page->addContent("<hr class=\"clearer\" />");
	$page->addContent($html);
	
	// outputting the html
	echo $page->getHTML();
?>
